// To parse this JSON data, do
//
//     final sport = sportFromJson(jsonString);

import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';

Sport sportFromJson(String str) => Sport.fromJson(json.decode(str));

String sportToJson(Sport data) => json.encode(data.toJson());

class Sport {
  final String sportId;
  final String sportName;
  final String sportDescription;
  final String? image;
  final String active;
  final String? createdBy;
  final String? updatedBy;
  final String isDeleted;
  final String? deletedBy;
  final DateTime? createAt;
  final DateTime? updateAt;
  final DateTime? deletedAt;
  File? imageFile;
  bool isSelected = false;

  Sport({
    this.sportId='-1',
    required this.sportName,
    this.sportDescription='',
    this.image,
    this.imageFile,
    this.active='1',
    this.createdBy,
    this.updatedBy,
    this.isDeleted ='0',
    this.deletedBy,
    this.createAt,
    this.updateAt,
    this.deletedAt,
  });

  Sport copyWith({
    String? sportId,
    String? sportName,
    String? sportDescription,
    String? image,
    String? active,
    String? createdBy,
    String? updatedBy,
    String? isDeleted,
    String? deletedBy,
    DateTime? createAt,
    DateTime? updateAt,
    DateTime? deletedAt,
  }) =>
      Sport(
        sportId: sportId ?? this.sportId,
        sportName: sportName ?? this.sportName,
        sportDescription: sportDescription ?? this.sportDescription,
        image: image ?? this.image,
        active: active ?? this.active,
        createdBy: createdBy ?? this.createdBy,
        updatedBy: updatedBy ?? this.updatedBy,
        isDeleted: isDeleted ?? this.isDeleted,
        deletedBy: deletedBy ?? this.deletedBy,
        createAt: createAt ?? this.createAt,
        updateAt: updateAt ?? this.updateAt,
        deletedAt: deletedAt ?? this.deletedAt,
      );

  factory Sport.fromJson(Map<String, dynamic> json) => Sport(
    sportId: json["sport_id"].toString(),
    sportName: json["sport_name"],
    sportDescription: json["sport_description"],
    image: json["image"],
    active: json["active"].toString(),
    createdBy: json["created_by"]?.toString(),
    updatedBy: json["updated_by"]?.toString(),
    isDeleted: json["is_deleted"].toString(),
    deletedBy: json["deleted_by"]?.toString(),
    createAt: DateTime.parse(json["create_at"]),
    updateAt: DateTime.parse(json["update_at"]),
    deletedAt: json["deleted_at"],
  );

  Future<Map<String, dynamic> > toJson() async=> {
    "sport_id": sportId,
    "sport_name": sportName,
    "sport_description": sportDescription,
    "image": image,
    "active": active,
    "created_by": createdBy,
    "updated_by": updatedBy,
    "is_deleted": isDeleted,
    "deleted_by": deletedBy,
    if(imageFile != null)
      'image':await MultipartFile.fromFile( imageFile!.path, filename: imageFile!.path.split('/').last ),
    "create_at": createAt?.toIso8601String(),
    "update_at": updateAt?.toIso8601String(),
    "deleted_at": deletedAt,
  };
}
